package com.mes.web.controller.cb;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.mes.entity.cb.PageBean;
import com.mes.entity.cb.PlanDetailed;
import com.mes.entity.tdd.Supplier;
import com.mes.service.cb.PlanDetailedService;
import com.mes.utils.DateJsonValueProcessor;
import com.mes.utils.ResponseUtil;

/**
 * @ClassName: PlanDetailedController 
 * @Description:采购详细
 * @Author cb
 * @Date 2017年12月21日 上午10:06:51 
 * @version 1.0
 */
@Controller
@RequestMapping("/PlanDetailed")
public class PlanDetailedController {
	
	@Resource
	private PlanDetailedService planDetailedService;
	
	@RequestMapping("/PlanDetailedList")
	public void PlanDetailedList(PageBean page,HttpServletResponse response,String offset,String limit) throws Exception {

		if (!"0".equals(offset)) {
			page.setPage(Integer.parseInt(offset)/Integer.parseInt(limit));
		}
		
		page.setPageSize(Integer.parseInt(limit));
		int total= planDetailedService.getTotal(page);
		List<PlanDetailed> planDetaileds = planDetailedService.PlanDetailedList(page);
		
		
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(planDetaileds, jsonConfig);
		jsonObject.put("total", total);
		jsonObject.put("rows", jsonArray);
		ResponseUtil.write(response, jsonObject);
		
	}
	
	/**
	 * 查询所有的计划单号
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/PlanDetailedAll")
	public void materieAll(HttpServletResponse response) throws Exception {
		List<PlanDetailed> planDetaileds=  planDetailedService.materieAll();
		Gson gson=new Gson();
		ResponseUtil.write(response, gson.toJson(planDetaileds));
	}
	
	/**
	 * 查询所有的供应商
	 * @throws Exception 
	 */
	@RequestMapping("/supperAdd")
	public void supperAdd(HttpServletResponse response) throws Exception {
		List<Supplier> suppliers=planDetailedService.supperAdd();
		Gson gson=new Gson();
		ResponseUtil.write(response, gson.toJson(suppliers));
	}
	
	@RequestMapping("/delete")
	public void materieDemandDelete(String ids,HttpServletResponse response) throws Exception {
       
		//删除
		int n = planDetailedService.planDetailedDelete(ids);
		//删除成功
		if (n >= 1) {
			response.getWriter().print("1");
			//未成功
		} else {
			response.getWriter().print("0");
			
		}
        	
        
	}
}
